package com.gxwy.shenzhou;

public class ZhuanZhangResponse implements StringHandler {

	private HeaderResponse header=new HeaderResponse();
	private String zhye;
	public static final int ZHYE_LEN=15;
	public static final int ZHYE_FLAG=1;
	private String fkwd;
	public static final int FKWD_LEN=60;
	public static final int FKWD_FLAG=2;
	private String weizhi;
	public static final int WEIZHI_LEN=255;
	public static final int WEIZHI_FLAG=2;
	public static final int TOTAL_LEN=ZHYE_LEN+FKWD_LEN+WEIZHI_LEN;
	private int reallen=0;
	public HeaderResponse getHeader() {
		return header;
	}

	public void setHeader(HeaderResponse header) {
		this.header = header;
	}

	public String getZhye() {
		return zhye;
	}

	public void setZhye(String zhye) throws Exception {
		if(zhye==null){
			Exception e=new Exception("String is null");
			throw e;
		}else if(zhye.getBytes("gbk").length<ZHYE_LEN){
			this.zhye=Supply.supply(zhye, ZHYE_FLAG, ZHYE_LEN);
		}else if(zhye.getBytes("gbk").length>ZHYE_LEN){
			 Exception e=new Exception("Too long");
			 throw e;
		}else{
			this.zhye = zhye;			
		}
	}

	public String getFkwd() {
		return fkwd;
	}

	public void setFkwd(String fkwd) throws Exception {
		if(fkwd==null){
			Exception e=new Exception("String is null");
			throw e;
		}else if(fkwd.getBytes("gbk").length<FKWD_LEN){
			this.fkwd=Supply.supply(fkwd, FKWD_FLAG, FKWD_LEN);
		}else if(fkwd.getBytes("gbk").length>FKWD_LEN){
			 Exception e=new Exception("Too long");
			 throw e;
		}else{
			this.fkwd = fkwd;			
		}
	}

	public String getWeizhi() {
		return weizhi;
	}

	public void setWeizhi(String weizhi) throws Exception {
		if(weizhi==null){
			Exception e=new Exception("String is null");
			throw e;
		}else if(weizhi.getBytes("gbk").length<WEIZHI_LEN){
			this.weizhi=Supply.supply(weizhi, WEIZHI_FLAG, WEIZHI_LEN);
		}else if(weizhi.getBytes("gbk").length>WEIZHI_LEN){
			 Exception e=new Exception("Too long");
			 throw e;
		}else{
			this.weizhi = weizhi;			
		}
	}

	public String build() throws Exception {
		if(this.getHeader()==null){
			throw new Exception("Header is null");
		}
		if(this.getZhye()==null){
			this.setZhye("");
		}
		if(this.getFkwd()==null){
			this.setFkwd("");
		}
		if(this.getWeizhi()==null){
			this.setWeizhi("");
		}
		Integer totallen=HeaderResponse.TOTAL_LEN+TOTAL_LEN-HeaderRequest.LEN_LEN;
		this.getHeader().setLen(totallen.toString());
		return this.getHeader().build()+this.getZhye()+this.getFkwd()+this.getWeizhi();
	}

	public void parse(String str) throws Exception {
		if(str==null){
			Exception e=new Exception("String is null");
			 throw e;
		}else if(str.getBytes("gbk").length<HeaderResponse.TOTAL_LEN+TOTAL_LEN){
			Exception e=new Exception("Invalid request");
			 throw e;
		}else{
			String headerLen=SplitString.splitString(str,reallen, reallen+HeaderResponse.TOTAL_LEN);
			reallen+=HeaderResponse.TOTAL_LEN;
			header.parse(headerLen);
			this.setZhye(SplitString.splitString(str, reallen, reallen+ZHYE_LEN));
			reallen+=ZHYE_LEN;
			this.setFkwd(SplitString.splitString(str, reallen, reallen+FKWD_LEN));
			reallen+=FKWD_LEN;
			this.setWeizhi(SplitString.splitString(str, reallen, reallen+WEIZHI_LEN));
			reallen+=WEIZHI_LEN;
		}
	}

}
